package models

import (
	"fmt"
	"gorm.io/gorm/schema"
)

type Organize struct {
	Id int64 `json:"id,omitempty"  gorm:"primaryKey"`
	SqlTimeFields
	SqlSaasTenantFields

	Name        string `json:"name" gorm:"type:varchar(64);"`
	Description string `json:"description,omitempty" gorm:"type:varchar(256)"`
	Type        string `json:"type" gorm:"type:varchar(16)"` //组织类型，自行定义,用于不同类型的组织区分
	Pid         int64  `json:"pid" gorm:"index"`

	Status int `json:"status,omitempty" gorm:"type:tinyint"` //组织状态，0：无，1：正常，2：禁用，其他自行定义
}

func (r *Organize) TableName(namer schema.Namer) string {
	return namer.TableName("organize")
}

func (r *Organize) NodeId() string {
	return fmt.Sprintf("%d", r.Id)
}

func (r *Organize) NodeName() string {
	return r.Name
}

func (r *Organize) NodePid() string {
	//空字符串代表根节点
	if r.Pid == 0 {
		return ""
	}
	return fmt.Sprintf("%d", r.Pid)
}

// OrganizeExtra 组织额外信息表
type OrganizeExtra struct {
	Id int64 `json:"id" gorm:"primaryKey"`
	SqlTimeFields

	OrgId   int64  `json:"org_id" gorm:"index"`
	Title   string `json:"title" gorm:"type:varchar(64)"`    //key-value形式的 key
	Content string `json:"content" gorm:"type:varchar(128)"` //key-value形式的 value
}

func (ad *OrganizeExtra) TableName(namer schema.Namer) string {
	return namer.TableName("organize_extra")
}

// OrganizeRelate 组织关系表
type OrganizeRelate struct {
	Id int64 `json:"id" gorm:"primaryKey"`
	SqlTimeFields

	OrgId     int64 `json:"org_id" gorm:"index"`     //组织id
	OrgParent int64 `json:"org_parent" gorm:"index"` //对应上级id
	Level     int   `json:"level"`                   //层级，最小为1，表示直属层级
}

func (relate *OrganizeRelate) TableName(namer schema.Namer) string {
	return namer.TableName("organize_relate")
}

func (relate *OrganizeRelate) NodeId() string {
	return fmt.Sprintf("%d", relate.Id)
}

func (relate *OrganizeRelate) NodeName() string {
	return fmt.Sprintf("%d", relate.OrgId)
}

func (relate *OrganizeRelate) NodePid() string {
	//空字符串代表根节点
	if relate.OrgParent == 0 {
		return ""
	}
	return fmt.Sprintf("%d", relate.OrgParent)
}
